package com.haotunet.app.goldtreasure.db;

import com.haotunet.app.goldtreasure.model.UserInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {
	private static String DBNAME = "datas.db";
	private static int VERSION = 1;
	private static DBOpenHelper dbHelper;
	private static final String TABLE_USER = "HJB_USER";
	private static String USER_ID = "userId";
	private static String REAL_NAME = "real_name";
	private static String MOBILE = "mobile";
	private static String ID_CARD = "id_card";
	private static String IS_LOGIN = "isLogin";
	private static final String CREATE_TABLE_USER = "create table " + TABLE_USER
			+ "(_id integer primary key autoincrement, " + IS_LOGIN + " text,"+USER_ID + " text," + REAL_NAME + " text," + MOBILE
			+ " text," + ID_CARD + " text" + ");";

	public DBOpenHelper(Context context) {
		super(context, DBNAME, null, VERSION);
	}

	public synchronized static DBOpenHelper getInstance(Context context) {
		if (dbHelper == null) {
			dbHelper = new DBOpenHelper(context);
		}
		return dbHelper;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_USER);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	private SQLiteDatabase getDatabase(boolean writable) {
		try {
			SQLiteDatabase database;
			if (writable) {
				database = getWritableDatabase();
			} else {
				database = getReadableDatabase();
			}
			return database;
		} catch (Exception e) {
			return null;
		}
	}

	// 用户表插入信息
	public synchronized long insertUser(String isLogin, UserInfo userInfo) {
		SQLiteDatabase database = getDatabase(true);
		if (null == database) {
			return -1;
		}

		ContentValues values = paseUserInfo(isLogin, userInfo);
		return database.insert(TABLE_USER, null, values);
	}

	// 用户表更新信息
	public synchronized long updateUser(String mobile, String isLogin, UserInfo userInfo) {
		SQLiteDatabase database = getDatabase(true);
		if (null == database) {
			return -1;
		}

		ContentValues values = paseUserInfo(isLogin, userInfo);

		return database.update(TABLE_USER, values, "mobile=?", new String[] { mobile });
	}

	// 用户表删除信息
	public synchronized void deleteValue(String mobile) {
		SQLiteDatabase database = getDatabase(true);

		database.delete(TABLE_USER, "mobile=?", new String[] { mobile });

	}

	// 用户表
	private ContentValues paseUserInfo(String isLogin, UserInfo userInfo) {
		ContentValues values = new ContentValues();
		values.put(IS_LOGIN, isLogin);
		values.put(USER_ID, userInfo.getUserId());
		values.put(REAL_NAME, userInfo.getRealName());
		values.put(MOBILE, userInfo.getUserName());
		values.put(ID_CARD, userInfo.getIdCard());
		return values;
	}
	public String getUserId() {
		SQLiteDatabase db = getDatabase(true);
		String sql = "select " + USER_ID + " from " + TABLE_USER + " where " + IS_LOGIN + "=0";
		Cursor cursor = db.rawQuery(sql, new String[] {});
		String userId = null;
		while (cursor.moveToNext()) {
			userId = cursor.getString(0);
		}
		if (userId == null) {
			userId = "";
		}
		cursor.close();
		return userId;
	}
	public String getMobile() {
		SQLiteDatabase db = getDatabase(true);
		String sql = "select " + MOBILE + " from " + TABLE_USER + " where " + IS_LOGIN + "=0";
		Cursor cursor = db.rawQuery(sql, new String[] {});
		String mobile = null;
		while (cursor.moveToNext()) {
			mobile = cursor.getString(0);
		}
		if (mobile == null) {
			mobile = "";
		}
		cursor.close();
		return mobile;
	}

	public String getRealName() {
		SQLiteDatabase db = getDatabase(true);
		String sql = "select " + REAL_NAME + " from " + TABLE_USER + " where " + IS_LOGIN + "=0";
		Cursor cursor = db.rawQuery(sql, new String[] {});
		String realName = null;
		while (cursor.moveToNext()) {
			realName = cursor.getString(0);
		}
		if (realName == null) {
			realName = "";
		}
		cursor.close();
		return realName;
	}

	public String getIdCard() {
		SQLiteDatabase db = getDatabase(true);
		String sql = "select " + ID_CARD + " from " + TABLE_USER + " where " + IS_LOGIN + "=0";
		Cursor cursor = db.rawQuery(sql, new String[] {});
		String id_card = null;
		while (cursor.moveToNext()) {
			id_card = cursor.getString(0);
		}
		if (id_card == null) {
			id_card = "";
		}
		cursor.close();
		return id_card;
	}

}
